#!/bin/bash

:<<EOF

按时间还原点恢复北部湾商户效户日期
在脚本服务器必须含有   /opt/trx-data-importer/config/mysql_client_prod.conf 这个配置文件


跑当天    bash  run_close_date_history.sh  -s 2025-08-01 -e   2025-08-01

跑历史    bash  run_close_date_history.sh  -s 2025-08-01 -e   2025-08-03

EOF


readonly  BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $BASE_DIR || exit;

shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] &&  alias date='gdate'


pwd
echo   $BASE_DIR"/close_date_sql.sh"
source  $BASE_DIR"/close_date_sql.sh"

CONFIG_FILE=/opt/trx-data-importer/config/mysql_client_prod.conf
start='2024-08-02'
# end='2025-07-01'
end='2025-08-20'



while getopts "Dfs:e:O:" opt; do
    case ${opt} in
    s )
        start="$OPTARG"
        ;;
    e )
        end="$OPTARG"
        ;;
    O )
        OUTPUT_DIR="$OPTARG"
        ;;
    D )
        DRY_RUN="YES"
        ;;
    f )
        FORCE_EXPORT="YES"
        FORCE_EXPORT_ARG=""
        ;;
    *)
        echo "Invalid option: $opt requires an argument" 1>&2
        ;;
    esac
done
shift $((OPTIND -1))

while ! [[ $start > $end ]]; do
    year_month=$(date -d  "${start}" +%Y%m)   #  yyyy-MM-dd  ==>  yyyymm
    day=$(date -d "$start" +%d)            #  yyyy-MM-dd   ==> dd
    echo "[$(date)] executing  ${year_month}/${day}"
    yesterday=$(date -d "$start - 1 day" +%F)

    execute_sql=$SQL
    execute_sql=${execute_sql//'#currentDay'/$start}
    execute_sql=${execute_sql//'#yesterday'/$yesterday}
    echo  "executing...........      $execute_sql"
  #  mysql --defaults-file=$CONFIG_FILE -e "$execute_sql"
    start=$(date -d "$start + 1 day" +%F)



done




